<template>
	<vk-data-dialog
		v-model="value.show"
		:title="page.title"
		:top="page.top"
		:width="page.width"
		:close-on-click-modal="true"
		mode="form"
		@open="onOpen"
		@closed="onClose"
	>
		<!-- 页面主体内容开始 -->
		<vk-data-form
			ref="form1"
			v-model="form1.data"
			:form-type="value.mode"
			:rules="form1.props.rules"
			:action="form1.props.action"
			:columns="form1.props.columns"
			:loading.sync="form1.props.loading"
			:labelWidth="form1.props.labelWidth"
			max-height="700px"
			:show-cancel="page.showCancel"
			:cancel-text="page.cancelText"
			:submit-text="page.submitText"
			@success="onFormSuccess"
		>

			<template v-slot:express_amount="{ form, keyName }">
				<view style="display: flex;" v-if="form.goods_sku_list">
					<view>订单运费：{{ vk.pubfn.toDecimal(form1.data.express_amount /100,2) }} 元</view>
					<view style="margin-left: 40px;">
						<text>商家优惠：</text>
						<vk-data-input-money v-model="form1.data.business_discount_express_amount" :max="vk.pubfn.toDecimal(form1.data.express_amount/100, 2)" :precision="0" :disabled="form1.data.express_amount<=0" placeholder="请输入金额" width="180px" rightText="元" @change="calcData"></vk-data-input-money>
					</view>

					<view style="margin-left: 40px;"><text>优惠后运费：</text><text style="color: red;font-weight: bold;">{{ vk.pubfn.toDecimal(form1.data.expressAmount / 100,2) }} 元</text></view>
				</view>
			</template>
			<template v-slot:total="{ form, keyName }">
				<view style="display: flex;" v-if="form.goods_sku_list">
					<view><text>商品数量：</text><text style="color: red;font-weight: bold;">{{ form1.data.goodsNum }}</text></view>
					<view style="margin-left: 40px;"><text>商家商品减价：</text><text style="color: red;font-weight: bold;">{{ vk.pubfn.priceFilter(form1.data.businessDiscountAmount) }}</text></view>
					<view style="margin-left: 40px;"><text>实付金额：</text><text style="color: red;font-weight: bold;">{{ vk.pubfn.priceFilter(form1.data.paymentAmount) }}</text></view>
				</view>
			</template>

		</vk-data-form>
		<!-- 页面主体内容结束 -->
	</vk-data-dialog>
</template>

<script>
var that; // 当前页面对象
var vk = uni.vk; // vk实例
export default {
	props: {
		value: {
			Type: Object,
			default: function() {
				return {
					show: false,
					mode: "",
					item: {}
				};
			}
		}
	},
	data: function() {
		// 组件创建时,进行数据初始化
		return {
			page: {
				title: "订单改价",
				submitText: "确定",
				cancelText: "关闭",
				showCancel: true,
				top: "7vh",
				width: "1220px"
			},
			form1: {
				// 表单请求数据，此处可以设置默认值
				data: {
					goods_sku_list:[]
				},
				// 表单属性
				props: {
					// 表单请求地址
					action: "admin/business/sys/order.updatePrice",
					// 表单字段显示规则
					columns: [
						{
						  key:"goods_sku_list", title:"商品信息", type:"array<object>", showLabel:false,
						  showAdd:false,
						  showClear:false,
						  showSort:false,
						  // 新增一行时,该行的默认值
						  defaultValue:{

						  },
						  rightBtns:[],
						  // 每行每个字段对应的渲染规则
						  columns:[
								{ key:"image", title:"商品图片", type:"image", width:100, limit:1, disabled: true },
								{ key:"goods_name", title:"商品名称", type:"text-view", minWidth:180, align:"left", showOverflowTooltip: true },
								{ key:"sku_name", title:"商品规格", type:"text-view", width:90 },
								{ key:"price", title:"商品原价", type:"money-view", width:90, align:"right" },
								{ key:"current_unit_price", title:"当前单价", type:"money-view", width:90, align:"right" },
								{ key:"business_discount_unit_amount", title:"商家优惠", type:"money", width:120, placeholder:"优惠金额", clearable:false,
									onChange:function(val, formData, column, index, option){
										if(formData.business_discount_unit_amount>formData.payment_unit_price){
											formData.business_discount_unit_amount = formData.payment_unit_price;
										}
										that.calcPriceItem(formData);
									}
								},
								{ key:"buy_num", title:"购买数量", type:"text-view", width:80, clearable:false,
									onChange:function(val, formData, column, index, option){
										that.calcPriceItem(formData);
									}
								},
								{ key:"total_amount", title:"原价合计", type:"money-view", width:90, align:"right" },
								{ key:"should_payment_amount_str", title:"实付合计", type:"text-view", width:150, align:"right" },
								{ key:"total_discount_amount", title:"合计优惠", type:"money-view", width:90, align:"right" },
						  ],
						},
						{ key:"express_amount", title:"运费", type:"money" },
						{ key:"total", title:"合计", type:"text" }
					],
					// 表单验证规则
					rules: {

					},
					// 左侧label的宽度
					labelWidth: "100px",
				}
			}
		};
	},
	mounted() {
		that = this;
		that.init();
	},
	methods: {
		// 初始化
		init() {
			let { value } = that;
			that.$emit("input", value);
		},
		// 监听 - 页面打开
		onOpen() {
			that = this;
			let { item={} } = that.value;
			// 每次打开时，重新设置表单的值 = value.item 的值，item通过 vk.pubfn.openForm('表单名',{ item:{ _id:"1" } }) 传递值
			item.goods_sku_list = that.calcPrice(item.goods_sku_list);
			let business_discount_express_amount = item.discount_info.business_discount_express_amount;
			if (!business_discount_express_amount) business_discount_express_amount = 0;
			that.form1.data = {
				_id:item._id,
				express_amount: item.amount_info.express_amount + business_discount_express_amount,
				business_discount_express_amount,
				goods_sku_list: vk.pubfn.copyObject(item.goods_sku_list)
			};
			that.calcData();
		},
		// 监听 - 页面关闭
		onClose() {
			//that.$refs.form1.resetForm(); // 关闭时，重置表单
		},
		// 监听 - 提交成功后
		onFormSuccess() {
			that.value.show = false; // 关闭页面
			that.$emit("success");
		},
		calcPrice(goods_sku_list){
			if(!goods_sku_list) goods_sku_list = that.form1.data.goods_sku_list;
			goods_sku_list.map((item, index) => {
				item.total_discount_amount = item.total_amount - item.should_payment_amount;
				item.should_payment_amount_str = vk.pubfn.priceFilter(item.should_payment_amount);
				if(item.total_amount>0){
					if(item.total_amount === item.should_payment_amount){
						item.should_payment_amount_str += ` (原价)`;
					}else{
						let real_discount_str = vk.pubfn.discountFilter(item.should_payment_amount / item.total_amount);
						item.should_payment_amount_str += ` (${real_discount_str})`;
					}
				}
				item.current_unit_price = parseInt(item.should_payment_amount / item.buy_num);
				if(item.business_discount_amount>0){
					item.business_discount_unit_amount = parseInt(item.business_discount_amount / item.buy_num);
					item.payment_unit_price = parseInt(item.current_unit_price + item.business_discount_unit_amount);
				}else{
					item.business_discount_unit_amount = "";
					item.payment_unit_price = item.current_unit_price;
				}
			});
			return goods_sku_list;
		},
		calcPriceItem(item){
			item.total_amount = item.price * item.buy_num;
			let { business_discount_unit_amount=0 } = item
			item.business_discount_amount = business_discount_unit_amount * item.buy_num;
			item.current_unit_price = item.payment_unit_price - business_discount_unit_amount;
			item.should_payment_amount = item.current_unit_price * item.buy_num;
			item.total_discount_amount = item.total_amount - item.should_payment_amount;
			item.should_payment_amount_str = vk.pubfn.priceFilter(item.should_payment_amount);
			if(item.total_amount>0){
				if(item.total_amount === item.should_payment_amount){
					item.should_payment_amount_str += `（原价）`;
				}else{
					let real_discount_str = vk.pubfn.discountFilter(item.should_payment_amount / item.total_amount);
					item.should_payment_amount_str += ` (${real_discount_str})`;
				}
			}
			that.calcData();
		},
		calcData(){
			let that = this;
			that.$set(that.form1.data,"goodsNum",that.goodsNumFn());
			that.$set(that.form1.data,"paymentAmount",that.paymentAmountFn());
			that.$set(that.form1.data,"businessDiscountAmount",that.businessDiscountAmountFn());
			that.$set(that.form1.data,"expressAmount",that.expressAmountFn());

		},
		goodsNumFn(){
			let that = this;
			let { goods_sku_list } = that.form1.data;
			let num = 0;
			goods_sku_list.map((item, index) => {
				num += item.buy_num;
			});
			return num;
		},
		// 计算订单实付金额
		paymentAmountFn(){
			let that = this;
			let { goods_sku_list } = that.form1.data;
			let paymentAmount = 0;
			goods_sku_list.map((item, index) => {
				paymentAmount += item.should_payment_amount;
			});
			paymentAmount += that.expressAmountFn();
			return paymentAmount;
		},
		// 计算商家合计商品优惠金额
		businessDiscountAmountFn(){
			let that = this;
			let { goods_sku_list } = that.form1.data;
			let businessDiscountAmount = 0;
			goods_sku_list.map((item, index) => {
				if(item.business_discount_amount){
					businessDiscountAmount += item.business_discount_amount;
				}
			});
			return businessDiscountAmount;
		},
		// 计算商家运费优惠金额
		expressAmountFn(){
			let { value } = this;
			let express_amount = that.form1.data.express_amount || 0;
			let business_discount_express_amount = that.form1.data.business_discount_express_amount || 0;
			return express_amount - business_discount_express_amount;
		}
	},
	watch: {

	},
	// 计算属性
	computed: {

	}
};
</script>

<style lang="scss" scoped></style>
